package com.alan.mongodb.elasticsearch;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import java.net.InetAddress;

/**
 * 使用时注入如下实例  TransportClient     client;
 * @Author Alan liu
 * @Date 18:16 2018/11/23
 * @return
 **/
public class TransportClientFactory implements FactoryBean<TransportClient>, InitializingBean, DisposableBean {

    private String clusterName;
    private String[]        clusterServers;
    private TransportAddress[] transportAddresses = null;
    private String host;
    private int             port;
    private TransportClient client;

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }

    public String[] getClusterServers() {
        return clusterServers;
    }

    public void setClusterServers(String[] clusterServers) {
        this.clusterServers = clusterServers;
        if (clusterServers != null && clusterServers.length > 0) {
            try {
                transportAddresses = new TransportAddress[clusterServers.length];
                for (int i = 0; i < clusterServers.length; i++) {
                    String server = clusterServers[i];
                    String[] serverInfo = server.split(":");
                    transportAddresses[i] = new TransportAddress(InetAddress
                            .getByName(serverInfo[0]), Integer.parseInt(serverInfo[1]));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void setHost(String host) {
        this.host = host;
    }

    public void setPort(int port) {
        this.port = port;
    }

    @Override
    public void destroy() throws Exception {
        if (client != null){
            client.close();
        }
    }

    @SuppressWarnings("resource")
    @Override
    public void afterPropertiesSet() throws Exception {
        Settings settings = Settings.builder()
                .put("cluster.name", this.clusterName)
                .put("transport.type","netty3")
                .put("http.type", "netty3")
                .build();
        client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress
                .getByName(this.host), this.port));
        if (transportAddresses != null && transportAddresses.length > 0) {
            client.addTransportAddresses(transportAddresses);
        }
    }

    @Override
    public TransportClient getObject() throws Exception {
        return client;
    }

    @Override
    public Class<?> getObjectType() {
        return TransportClient.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}